<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>把第一个参数作为回调函数调用</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.call-user-func-array.html">« call_user_func_array</a></li>
      <li style="float: right;"><a href="function.create-function.html">create_function »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.funchand.html">函数处理 函数</a></li>
    <li>把第一个参数作为回调函数调用</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.call-user-func" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">call_user_func</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">call_user_func</span> &mdash; <span class="dc-title">把第一个参数作为回调函数调用</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.call-user-func-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>call_user_func</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$args</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   第一个参数 <code class="parameter">callback</code> 是被调用的回调函数，其余参数是回调函数的参数。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.call-user-func-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
       将被调用的回调函数（<span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span>）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">args</code></dt>

     <dd>

      <p class="para">
       0个或以上的参数，被传入回调函数。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        请注意，传入<span class="function"><strong>call_user_func()</strong></span>的参数不能为引用传递。
        <div class="example" id="example-5073">
         <p><strong>示例 #1 <span class="function"><strong>call_user_func()</strong></span> 的参考例子</strong></p>
         <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br />function&nbsp;</span><span style="color: #0000BB">increment</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$var</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$var</span><span style="color: #007700">++;<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'increment'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;it&nbsp;is&nbsp;possible&nbsp;to&nbsp;use&nbsp;this&nbsp;instead<br /></span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #DD0000">'increment'</span><span style="color: #007700">,&nbsp;array(&amp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;it&nbsp;is&nbsp;also&nbsp;possible&nbsp;to&nbsp;use&nbsp;a&nbsp;variable&nbsp;function<br /></span><span style="color: #0000BB">$increment&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'increment'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$increment</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
         </div>

         <div class="example-contents"><p>以上例程会输出：</p></div>
         <div class="example-contents screen">
<div class="cdata"><pre>
Warning: Parameter 1 to increment() expected to be a reference, value given in …
0
1
2
</pre></div>
         </div>
        </div>
       </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.call-user-func-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回回调函数的返回值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.call-user-func-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5074">
    <p><strong>示例 #2 <span class="function"><strong>call_user_func()</strong></span> 的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">barber</span><span style="color: #007700">(</span><span style="color: #0000BB">$type</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"You&nbsp;wanted&nbsp;a&nbsp;</span><span style="color: #0000BB">$type</span><span style="color: #DD0000">&nbsp;haircut,&nbsp;no&nbsp;problem\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'barber'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"mushroom"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'barber'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"shave"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
You wanted a mushroom haircut, no problem
You wanted a shave haircut, no problem
</pre></div>
    </div>
   </div>
   <div class="example" id="example-5075">
    <p><strong>示例 #3 <span class="function"><strong>call_user_func()</strong></span> 命名空间的使用</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">Foobar</span><span style="color: #007700">;<br /><br />class&nbsp;</span><span style="color: #0000BB">Foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;world!\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #0000BB">__NAMESPACE__&nbsp;</span><span style="color: #007700">.</span><span style="color: #DD0000">'\Foo::test'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #0000BB">__NAMESPACE__&nbsp;</span><span style="color: #007700">.</span><span style="color: #DD0000">'\Foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Hello world!
Hello world!
</pre></div>
    </div>
   </div>
   <div class="example" id="example-5076">
    <p><strong>示例 #4 用<span class="function"><strong>call_user_func()</strong></span>来调用一个类里面的方法</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">myclass&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">say_hello</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Hello!\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$classname&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"myclass"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$classname</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'say_hello'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #0000BB">$classname&nbsp;</span><span style="color: #007700">.</span><span style="color: #DD0000">'::say_hello'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$myobject&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">myclass</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$myobject</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'say_hello'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Hello!
Hello!
Hello!
</pre></div>
    </div>   
   </div>
   <div class="example" id="example-5077">
    <p><strong>示例 #5 把完整的函数作为回调传入<span class="function"><strong>call_user_func()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />call_user_func</span><span style="color: #007700">(function(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)&nbsp;{&nbsp;print&nbsp;</span><span style="color: #DD0000">"[</span><span style="color: #0000BB">$arg</span><span style="color: #DD0000">]\n"</span><span style="color: #007700">;&nbsp;},&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
[test]
</pre></div>
    </div>   
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.call-user-func-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">在函数中注册有多个回调内容时(如使用
<span class="function"><strong>call_user_func()</strong></span> 与 <span class="function"><a href="function.call-user-func-array.html" class="function">call_user_func_array()</a></span>)，如在前一个回调中有未捕获的异常，其后的将不再被调用。</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.call-user-func-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.call-user-func-array.html" class="function" rel="rdfs-seeAlso">call_user_func_array()</a> - 调用回调函数，并把一个数组参数作为回调函数的参数</span></li>
    <li class="member"><span class="function"><a href="function.is-callable.html" class="function" rel="rdfs-seeAlso">is_callable()</a> - 验证值是否可以在当前范围内作为函数调用。</span></li>
    <li class="member"><a href="functions.variable-functions.html" class="link">Variable functions</a></li>
    <li class="member"><span class="methodname"><a href="reflectionfunction.invoke.html" class="methodname" rel="rdfs-seeAlso">ReflectionFunction::invoke()</a> - Invokes function</span></li>
    <li class="member"><span class="methodname"><a href="reflectionmethod.invoke.html" class="methodname" rel="rdfs-seeAlso">ReflectionMethod::invoke()</a> - Invoke</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>